package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Properties;
import oppsa.controller.Translate;
import oppsa.model.SubsystemDB;
import oppsa.util.Constants;
import oppsa.util.Session;

import org.jdesktop.swingx.JXFrame;

public class AddPortfolio extends JXFrame  {
	private AuthentificationView authview;
	private JTextField textFieldPortfolio;
	private JPanel panel;
	private JButton btnSubmit;
	private JButton btnCancel;
	private JButton source;
	private SubsystemDB listSubsystemDB;
	private JButton btnAddSubSystem;
	private SubsystemDB listSubSystem;
	private Properties prop;
	
	public AddPortfolio(AuthentificationView authentificationView) {
		// TODO Auto-generated constructor stub
		addWindowListener(closeWindow1);
		final Translate t = new Translate();
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddSSComponentBox.class.getResource("/LOGO_FONCE.png")));
		setStartPosition(StartPosition.CenterInScreen);
		authview=authentificationView;
		setMinimumSize(new Dimension(280, 150));
		setResizable(false);
		setTitle(t.getLocalizedText("tlAddPortfolioWindow"));
		prop = new Properties();
    	prop.loadProperties(Constants.config_path);

		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));
		JLabel lblAddPortfolio = new JLabel(t.getLocalizedText("lblAddPortfolio"));
		lblAddPortfolio.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddPortfolio, "cell 0 0 2 1");
		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
		btnSubmit.setBorder(null);
		
		textFieldPortfolio = new JTextField();
		textFieldPortfolio.setColumns(20);
		panel.add(textFieldPortfolio, "cell 0 1,alignx left");
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {

				Boolean exist=false;
				int i = 0;
				File folder = new File(prop.getProperties().getProperty("path")+"/bdd");
			    File[] listOfFiles = folder.listFiles();

				while (i < listOfFiles.length && !exist) {
					if (listOfFiles[i].isFile() && (listOfFiles[i].getName().equals(textFieldPortfolio.getText()))) {
						exist=true;
					}
					i++;
				}
				if (!exist)
				{
					authview.setVisible(true);
					authview.setEnabled(true);
					authview.toFront();
					Session.currentBDD=textFieldPortfolio.getText();
					authview.getComboxModel().addElement(textFieldPortfolio.getText());
					setVisible(false);

				} else {
					JOptionPane.showMessageDialog(AddPortfolio.this,t.getLocalizedText("lblDuplicateTree"),"composant",JOptionPane.INFORMATION_MESSAGE);
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});

		
		

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				
				authview.setVisible(true);
				authview.setEnabled(true);
				authview.toFront();
				setVisible(false);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
			}
		});
		panel.add(btnSubmit, "cell 0 3");
		panel.add(btnCancel, "cell 0 3");
		
		
		
		
	}
	
	WindowAdapter closeWindow1 = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			authview.setVisible(true);
			authview.setEnabled(true);
			authview.toFront();	
		}
	};

}
